package com.group.eams.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.eams.pojo.dto.FixedAssetCategoryQueryDto;
import com.group.eams.pojo.entity.FixedAssetCategory;
import com.group.eams.service.FixedAssetCategoryService;
import com.group.pojo.dto.BatchDeleteDto;
import com.group.result.Result;
import com.group.result.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 固定资产分类表(FixedAssetCategory)表控制层
 *
 * @author DHUser
 * @since 2024-10-30 12:58:28
 */
@RestController
@RequestMapping("/assetCategory")
public class FixedAssetCategoryController  {
    /**
     * 服务对象
     */
    @Autowired
    private FixedAssetCategoryService fixedAssetCategoryService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping(value = "/find",method = RequestMethod.GET)
    public Result findById(@RequestParam Long id){
        FixedAssetCategory fixedAssetCategory = fixedAssetCategoryService.getById(id);
        return Result.buildSuccess(fixedAssetCategory);
    }

    /**
     * 分页查询
     *
     * @param fixedAssetCategoryQueryDto 筛选条件
     * @return 查询结果
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result list(FixedAssetCategoryQueryDto fixedAssetCategoryQueryDto) {
        Page<FixedAssetCategory> fixedAssetCategoryPage = fixedAssetCategoryService.findByQueryDto(fixedAssetCategoryQueryDto);
        return Result.buildSuccess(fixedAssetCategoryPage.getTotal(), fixedAssetCategoryPage.getRecords());
    }

    /**
     * 查询所有数据
     *
     * @return 所有数据
     */
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Result findAll() {
        List<FixedAssetCategory> list = fixedAssetCategoryService.list();
        return Result.buildSuccess(list);
    }

    /**
     * 新增数据
     *
     * @param fixedAssetCategory 实体
     * @return 新增结果
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody FixedAssetCategory fixedAssetCategory){
        boolean result = fixedAssetCategoryService.save(fixedAssetCategory);
        return result ? Result.buildSuccess() : Result .buildFail(ResultEnum.OPTION_FAIL);
    }

    /**
     * 批量添加
     * @param fixedAssetCategory
     * @return
     */
    @RequestMapping(value = "/addBatch",method = RequestMethod.POST)
    public Result addBatch(@RequestBody List<FixedAssetCategory> fixedAssetCategory){
        boolean result = fixedAssetCategoryService.saveBatch(fixedAssetCategory);
        return result ? Result.buildSuccess() : Result .buildFail(ResultEnum.OPTION_FAIL);
    }

    /**
     * 编辑数据
     *
     * @param fixedAssetCategory 实体
     * @return 编辑结果
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(@RequestBody FixedAssetCategory fixedAssetCategory){
        boolean result = fixedAssetCategoryService.updateById(fixedAssetCategory);
        return result ? Result.buildSuccess() : Result .buildFail(ResultEnum.OPTION_FAIL);
    }

    /**
     * 批量编辑
     * @param fixedAssetCategory
     * @return
     */
    @RequestMapping(value = "/updateBatch",method = RequestMethod.POST)
    public Result updateBatch(@RequestBody List<FixedAssetCategory> fixedAssetCategory){
        boolean result = fixedAssetCategoryService.updateBatchById(fixedAssetCategory);
        return result ? Result.buildSuccess() : Result .buildFail(ResultEnum.OPTION_FAIL);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public Result delete(@RequestParam Long id){
        boolean result = fixedAssetCategoryService.removeById(id);
        return result ? Result.buildSuccess() : Result .buildFail(ResultEnum.OPTION_FAIL);
    }

    /**
     * 批量删除
     * @param batchDeleteDto
     * @return
     */
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.POST)
    public Result deleteBatch(@RequestBody BatchDeleteDto batchDeleteDto) {
        boolean result = fixedAssetCategoryService.removeByIds(batchDeleteDto.getIds());
        return result ? Result.buildSuccess() : Result .buildFail(ResultEnum.OPTION_FAIL);
    }
}

